package com.vtmer.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vtmer.entity.Progress;
import com.vtmer.entity.RegistrationForm;
import com.vtmer.mapper.RegistrationFormMapper;
import com.vtmer.service.ProgressService;
import com.vtmer.service.RegistrationFormService;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Xinjie
 * @create 2022/9/26 10:37
 */
@Service
public class RegistrationFormServiceImpl extends ServiceImpl<RegistrationFormMapper, RegistrationForm> implements RegistrationFormService {
    @Resource
    RegistrationFormMapper registrationFormMapper;

    private final LambdaUpdateWrapper<RegistrationForm> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();

    @Autowired
    ProgressService progressService;

    @Override
    public RegistrationForm getFormByUid(int uid) {
        return getOne(new QueryWrapper<RegistrationForm>().eq("reg_user_id", uid));
    }

    @Override
    public List<RegistrationForm> getFormList() {
        return registrationFormMapper.selectList(null).size() == 0 ? null : registrationFormMapper.selectList(null);
    }

    @Override
    public List<RegistrationForm> getFormByGroup(String groups) {
        List<RegistrationForm> registrationForms = registrationFormMapper.selectList(new QueryWrapper<RegistrationForm>().eq("interest_groups", groups));
        return registrationForms.size() == 0 ? null : registrationForms;
    }


    @Override
    public RegistrationForm getRegistrationFormById(int uid) {
        return getOne(new QueryWrapper<RegistrationForm>().eq("user_id", uid));
    }

    @Override
    public RegistrationForm getRegistrationFormByName(String name) {
        return getOne(new QueryWrapper<RegistrationForm>().eq("username",name));
    }

    @Override
    public RegistrationForm getRegistrationFormByStudId(String studId) {
        return getOne(new QueryWrapper<RegistrationForm>().eq("stud_id",studId));
    }


    @Override
    public boolean reg(RegistrationForm registrationForm) {


        QueryWrapper<RegistrationForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stud_id", registrationForm.getStudId());

        //如果是ture 抛出异常
        Assert.isFalse(registrationFormMapper.exists(queryWrapper), "您已填写过报名表");
            int rows = registrationFormMapper.insert(registrationForm);
            if (rows != 1) {
                throw new IllegalArgumentException("出现未知错误，请联系管理员");
            }else{
                progressService.insertProgress(new Progress(registrationForm.getStudId()));
                return true;
            }
    }

    public void updateRegFormInfo(String studId,int state,int fail){
        lambdaUpdateWrapper.eq(RegistrationForm::getStudId,studId)
                .set(RegistrationForm::getState,state)
                .set(RegistrationForm::getFail,fail);
        registrationFormMapper.update(null,lambdaUpdateWrapper);
        lambdaUpdateWrapper.clear();
    }

    @Override
    public RegistrationForm getProgressByStudIdAndName(long studId) {
        return registrationFormMapper.selectOne(
                new QueryWrapper<RegistrationForm>().eq("stud_id", studId));
    }

}
